<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="header-right-contaier">
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button class="logout-btn" type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边菜单栏区域 unique-opend未生效，还未解决-->
        <el-menu background-color="#9FA8DA" text-color="#fff"
         active-text-color="#5C6BC0" unique-opend :collapse="isCollapse"
         :collapse-transition="false" :router="true" :default-active="activePath">
          <!-- 一级菜单 -->
           <!-- 这里的 index 必须是一个字符串 -->
          <el-submenu v-for="item in menuList" :index="item.id.toString()" :key="item.id">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item v-for="subItem in item.children" :index="'/' + subItem.path" :key="subItem.id"
             @click="saveNavState('/' + subItem.path)">
              <!-- 二级菜单的模板区 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>

          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单栏数据
      menuList: [],
      iconsList: [
        { iconName: 'iconfont icon-user' },
        { iconName: 'iconfont icon-tijikongjian' },
        { iconName: 'iconfont icon-shangpin' },
        { iconName: 'iconfont icon-danju' },
        { iconName: 'iconfont icon-baobiao' }
      ],
      iconsObj: {},
      // 是否折叠侧边菜单
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.setMenuIcon(res.data)
      this.menuList = res.data
      // console.log(this.menuList)
    },
    // 处理一级表单的icon
    setMenuIcon (arr) {
      if (arr.length !== 0) arr.map((item, index) => { this.iconsObj[item.id] = this.iconsList[index].iconName; return this.iconsObj })
      // console.log(this.iconsObj)
    },
    // 点击按钮，切换侧边菜单的折叠和展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.logout-btn {
  background-color: #5C6BC0;
  border: none;
}

.el-header {
  background-color: #7986CB;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  .header-right-contaier {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #9FA8DA;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #f0f5ff;
}

.home-container {
  height: 100%;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #C5CAE9;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
